package net.zdreamer.geracomium.adapter;

import java.util.ArrayList;
import java.util.List;

import net.zdreamer.geracomium.ui.HomeFragment;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.geracomium.R;

/** 
 */
public class HomepageViewPagerAdapter extends PagerAdapter {

	// 界面列表
	List<String> ImageURL; /* 要加载的图片地址 */
	private List<View> views;
	private Activity activity;
	private static final String SHAREDPREFERENCES_NAME = "first_pref";

	public HomepageViewPagerAdapter(Fragment fragment) {
		this.activity = fragment.getActivity();

		/*
		 * test code
		 */
		views = new ArrayList<View>();
		ImageView img1 = new ImageView(fragment.getActivity());
		img1.setBackgroundResource(R.drawable.zhuyeguanggao1);
		views.add(img1);

		ImageView img2 = new ImageView(fragment.getActivity());
		img2.setBackgroundResource(R.drawable.zhuyeguanggao2);
		views.add(img2);

		ImageView img3 = new ImageView(fragment.getActivity());
		img3.setBackgroundResource(R.drawable.zhuyeguanggao3);
		views.add(img3);

		ImageView img4 = new ImageView(fragment.getActivity());
		img4.setBackgroundResource(R.drawable.zhuyeguanggao4);
		views.add(img4);
		
		ImageView img5 = new ImageView(fragment.getActivity());
		img4.setBackgroundResource(R.drawable.zhuyeguanggao4);
		views.add(img5);

	}
	@Override
    public int getCount() {
        // TODO Auto-generated method stub
        return views.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        // TODO Auto-generated method stub
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // TODO Auto-generated method stub
        ((ViewPager) container).removeView(views.get(position));
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
//        ImageView imageView = new ImageView(activity);
    	ImageView imageView = new ImageView(activity);
		imageView.setBackgroundResource(position == 0 ? R.drawable.zhuyeguanggao1 :
										position == 1 ? R.drawable.zhuyeguanggao2 :
										position == 2 ? R.drawable.zhuyeguanggao3 :
										position == 3 ? R.drawable.zhuyeguanggao4 :
										R.drawable.zhuyeguanggao5);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setPadding(5, 0, 8, 0);
//        ImageLoader imageLoader = new ImageLoader(queue, imageCache);
//        ImageListener imageListener =
//                imageLoader.getImageListener(imageView, R.drawable.loading53, R.drawable.loading53);
//        imageLoader.get(mListviews.get(position), imageListener);

//        imageView.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View arg0) {
//                // Toast.makeText(context, "广告位跳转", Toast.LENGTH_SHORT).show();
//                if (null != mOnItemClickListener) {
//                    mOnItemClickListener.onItemClickListener(position);
//                }
//
//            }
//        });

        container.addView(imageView);
        return imageView;
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemOnClickListener(OnItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    public interface OnItemClickListener {
        public void onItemClickListener(int position);
    }

}
